package com.situ.blog.dao.impl;

import com.situ.blog.dao.IUserDao;
import com.situ.blog.pojo.entity.User;
import com.situ.blog.util.JDBCUtils2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements IUserDao {
    @Override
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtils2.getConnection();
            String sql = "select id,name,password,role,nick_name,status from user where name=? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, password);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                int role = resultSet.getInt("role");
                String nickName = resultSet.getString("nick_name");
                int status = resultSet.getInt("status");
                user = new User();
                user.setId(id);
                user.setName(name);
                user.setNickName(nickName);
                user.setStatus(status);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }
}